package leetcode;

public class DivideTwoIntegers {

	public int divide(int dividend, int divisor) {  
        int sign = 1;  
        if(dividend<0) sign = -sign;  
        if(divisor<0) sign = -sign;  
        long temp = Math.abs((long)dividend);  
        long temp2 = Math.abs((long)divisor);  
        long c = 1;  
        while(temp>temp2){  
            temp2 = temp2<<1;  
            c = c<<1;  
        }  
        long res = 0;  
        while(temp>=Math.abs((long)divisor)){  
            if(temp>=temp2){  
                temp-=temp2;
                  res+=c;
            }  
            temp2 = temp2>>1;  
            c=c>>1;  
        }  
        if(sign>0){
          if(res>Integer.MAX_VALUE){
        	  return Integer.MAX_VALUE;
          }else{
        	  return (int) res;
          }	
        }else{
        	if(res>Integer.MAX_VALUE+(long)1)
        		return Integer.MIN_VALUE;
        	else return -(int)res;
        }
    }
	public static void main(String[] args) {
		DivideTwoIntegers d=new DivideTwoIntegers();
		d.divide(-2147483648, 1);
	}
}
